//
//  TicketMerchantViewController.m
//  myhuibao
//
//  Created by 孙昕 on 14-3-11.
//  Copyright (c) 2014年 myhuibao. All rights reserved.
//

#import "TicketMerchantViewController.h"
#import "SBJson.h"
#import "TicketMerchantCell.h"
#import "ASIHTTPRequest.h"
#import "TicketDetailViewController.h"
#import "Config.h"
#import "OMGToast.h"

@interface TicketMerchantViewController ()

@end

@implementation TicketMerchantViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _lbMerchantName.text=_MerchantName;
    [self getTicket];
    // Do any additional setup after loading the view from its nib.
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)getTicket
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@&merchantType=%d&merchantID=%d",url_merchantTicketList,_MerchantType,_MerchantId]];
    ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:url];
    [req setShouldAttemptPersistentConnection:NO];
    [req setDelegate: self];
    [req setDidFinishSelector:@selector(finishGetTicket:)];
    
    [req startAsynchronous];
}

- (void)finishGetTicket:(ASIHTTPRequest *)request
{
    NSString *jsonStr = [request responseString];
    NSDictionary *dic = [jsonStr JSONValue];
    tableDataArray = [dic valueForKey:@"data"];
    if((NSNull*)tableDataArray==[NSNull null] || [tableDataArray count]==0)
    {
        [OMGToast showWithText:@"该商户没有可用的优惠券！" bottomOffset:100];
    }
    [_TicketTable reloadData];
    [Config setExtraCellLineHidden:_TicketTable];
}

- (IBAction)OnBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	int count = [tableDataArray count];
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIdentifier = @"TicketMerchantCellIdentifier";
        
    TicketMerchantCell *cell = (TicketMerchantCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"TicketMerchantCell" owner:nil options:nil];
        cell = [array objectAtIndex:0];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    }
    NSMutableDictionary *item = [tableDataArray objectAtIndex:indexPath.row];
    cell.lbTitle.text=item[@"title"];
    cell.lbCount.text=[NSString stringWithFormat:@"下载次数:%d",[item[@"downloadNum"] intValue]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
        NSMutableDictionary *item = [tableDataArray objectAtIndex:indexPath.row];
        TicketDetailViewController *view=[[TicketDetailViewController alloc] initWithNibName:@"TicketDetailViewController" bundle:nil];
        view.hidesBottomBarWhenPushed=YES;
        view.merchantTypeID=[[item objectForKey:@"merchantTypeID"] intValue];
        view.couponID=[[item objectForKey:@"couponID"] intValue];
        [self.navigationController pushViewController:view animated:YES];
}
@end
